package com.kaka.bus.engine.evaluate.sell;

import com.kaka.bus.bean.HttpWrapper;
import com.kaka.bus.bean.ProductInfo;
import com.kaka.bus.di.DataManager;
import com.kaka.bus.engine.RouterUtil;
import com.kaka.bus.manager.Result;
import com.kaka.bus.manager.T;
import com.kaka.bus.mvp.RxPresenter;

import javax.inject.Inject;

/**
 * Created by OnionMac on 2018/1/17.
 */

public class SellPresenter extends RxPresenter<SellContract.View> implements SellContract.Presenter{

    @Inject
    public SellPresenter(DataManager dataManager) {
        super(dataManager);
    }

    @Override
    public void loadData(String userId, String token, String device,String productId) {

        mView.showDialog("稍等..");
        addSubscribe(mDataManager
                .getApi()
                .getProduct(userId,token,device,productId)
                .compose(T.D())
                .subscribeWith(new Result<HttpWrapper<ProductInfo>>() {
                    @Override
                    protected void onSuccess(HttpWrapper<ProductInfo> wrapper) {
                        mView.getProductSuccess(wrapper);
                    }

                    @Override
                    protected void onFinish() {
                        super.onFinish();
                        mView.dissDialog();
                    }
                }));

    }

    @Override
    public void createProduct(String userId, String productId, String amount, String token) {
        mView.showDialog("稍等..");
        addSubscribe(mDataManager
                .getApi()
                .createProduct(userId,productId,amount,token)
                .compose(T.D())
                .subscribeWith(new Result<HttpWrapper<String>>() {
                    @Override
                    protected void onSuccess(HttpWrapper<String> wrapper) {
                        mView.createProduct(wrapper);
                    }

                    @Override
                    protected void onFinish() {
                        super.onFinish();
                        mView.dissDialog();
                    }

                }));
    }

    @Override
    public void router(SellActivity sellActivity) {
        mView.showDialog("查询节点中...");
        RouterUtil.router(sellActivity, mDataManager.getApi(),mCompositeDisposable, () -> {
            if(mView != null){
                mView.dissDialog();
            }
        });
    }
}
